<?php

namespace app\admin\controller;


use app\admin\model\Ht;
use cmf\controller\AdminBaseController;
use think\facade\Db;

/**
 * Class HtController
 * @package app\admin\controller
 * @adminMenuRoot(
 *     'name'   =>'合同管理',
 *     'action' =>'index',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>''
 * )
 */
class HtController extends AdminBaseController
{

    public function index()
    {
        //搜索部分
//        $key_status = input('get.key_status/d');

        $lists = Ht::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }
        $lists = $lists->where('delete_time', 0);
        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);
        $companytypea_arr = array(1 => '销售', 2 => '来料',3=>'委外',4=>'集团内销售');
        $this->assign('companytypea_arr', $companytypea_arr);
        $user_arr = Db::name('user')
            ->column('user_nickname', 'id');

        $this->assign('user_arr', $user_arr);
        $grade_id_arr = Db::name('grade')

            ->column('g_name', 'id');
//        var_dump($grade_id_arr);
        $this->assign('grade_id_arr', $grade_id_arr);
        $company_id_arr = Db::name('company')

            ->column('name', 'id');
//            Db::query('select id,name from cmf_company where delete_time =0');
        $this->assign('company_id_arr', $company_id_arr);

        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(), 'domain' => request()->domain() . '/upload/']);
    }
    /**
     *  @adminMenu(
     *     'name'   => '新增',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '新增',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function add()
    {
        $companytypea_arr=array(0=>array('id'=>1,'name'=>'自主'),1=>array('id'=>2,'name'=>'来料'), 2 => array('id' => 3, 'name' => '委外'),3 => array('id' => 4, 'name' => '集团内销售'));
        $this->assign('companytypea_arr', $companytypea_arr);

        $ht_grade_arr = Db::query('select id,g_name from cmf_grade where delete_time =0');
        $this->assign('ht_grade_arr', $ht_grade_arr);
        $company_id_arr = Db::query('select id,name from cmf_company where delete_time =0');
        $this->assign('company_id_arr', $company_id_arr);
        return $this->fetch('', ['domain' => request()->domain() . '/upload/']);
    }
    /**
     *  @adminMenu(
     *     'name'   => '新增提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '新增提交',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function addPost()
    {
//        $this->error('操作成功');
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        if (!$params) {
            $this->error('非法参数');
        }
        $params['delivery_time'] = strtotime($params['delivery_time']);
        $params['update_time'] = time();
        $params['add_time'] = time();
//        $params['ht_sheets']=$params['ht_tw']/(($params['ht_d']*($params['ht_w']/1000)*($params['ht_h']/1000)*7.85)/1000);
        $params['admin_id']=cmf_get_current_admin_id();
        $id= Db::name('ht')->insertGetId($params);
        $params['id']=$id;
//        cmf_install_log($params,'ht_log','add');
//        Db::name('ht_log')->insertGetId()

        $this->success('操作成功', 'index');
    }
    /**
     *  @adminMenu(
     *     'name'   => '修改',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function edit()
    {
        $id = input('get.id/d');

        if (!is_numeric($id)) {
            $this->error('非法参数');
        }
        $companytypea_arr=array(0=>array('id'=>1,'name'=>'自主'),1=>array('id'=>2,'name'=>'来料'), 2 => array('id' => 3, 'name' => '委外'),3 => array('id' => 4, 'name' => '集团内销售'));
        $this->assign('companytypea_arr', $companytypea_arr);

        $res = Ht::where('id', $id)->find();
        $ht_grade_arr = Db::query('select id,g_name from cmf_grade where delete_time =0');
        $this->assign('ht_grade_arr', $ht_grade_arr);
        $company_id_arr = Db::query('select id,name from cmf_company where delete_time =0');
        $this->assign('company_id_arr', $company_id_arr);

        return $this->fetch('', ['vo' => $res, 'domain' => request()->domain() . '/upload/']);
    }
    /**
     *  @adminMenu(
     *     'name'   => '修改提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改提交',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function editPost()
    {

        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        if (!$params || empty($params['id'])) {
            $this->error('非法参数');
        }
//        $params['ht_sheets']=$params['ht_tw']/(($params['ht_d']*($params['ht_w']/1000)*($params['ht_h']/1000)*7.85)/1000);

        $params['delivery_time'] = strtotime($params['delivery_time']);
        $params['update_time'] = time();
        $params['admin_id']=cmf_get_current_admin_id();
        Ht::update($params);
        cmf_install_log($params,'ht_log','update');
        $this->success('操作成功', 'index');
    }

    /**
     *  @adminMenu(
     *     'name'   => '删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function del()
    {
        $id = input('get.id/d');

        if (!(is_numeric($id) && $id > 0)) {
            $this->error('非法参数');
        }


        Ht::where('id', $id)->update(array('delete_time' => time(),'admin_id'=>cmf_get_current_admin_id()));
        $params= Db::name('ht')->where('id',$id)->find();
        cmf_install_log($params,'ht_log','del');
        $this->success('删除成功');
    }

    public function field()
    {
        $id = input('get.id/d');
        $field = input('get.field');

        if (!(is_numeric($id) && $id > 0 && $field)) {
            $this->error('非法参数');
        }

        $info = Ht::field($field)->where('id', $id)->find();
        $info[$field]=explode(',',$info[$field]);
        $list = [];
        foreach ($info[$field] as $k => $v) {
//            var_dump($v);
            $asset = Db::name('asset')->where('file_path', $v)->find();
            if ($asset) {
                $list[$k] = $asset;
            } else {
                if ($v != '') {
                    $list[$k]['filename'] = $v;
                    $list[$k]['file_path'] = $v;
                }
            }
        }
        $this->success('ok', '', $list);
    }
    public function findid(){
        $id = input('get.id/d');
        $res = Ht::where('id', $id)->find();
        if($res){
            $res['delivery_time']=date('Y-m-d H:i',$res['delivery_time']);
            $this->success('ok', '', $res);
        }else{
            $this->error('查询失败请联系管理员');
        }
    }
    /**excel 导出
     *      *   @adminMenu(
     *     'name'   => 'excel导出',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '接收计划',
     *     'param'  => ''
     * )
     * @return void
     */
    public function outexcel()
    {
        //要导出的数据,二维
        $lists = Ht::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }
        $lists = $lists->where('delete_time', 0);
        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);

        $data = $lists->toArray();
        $data = $data['data'];
        $head = array( '合同编号',
            '归属类型',
            '合同名称',
            '客户名称',
            '牌号',
            '厚',
            '宽',
            '长',
            '重量',
            '张数',
            '交货时间',
            '备注',
            '添加时间',
            '更新时间',
            '最后操作人',);

        //数据中对应的字段，用于读取相应数据：
        $keys = [
            'ht_number',
            'companytype',
            'ht_name',
            'company_id',
            'ht_grade_id',
            'ht_d',
            'ht_w',
            'ht_h',
            'ht_tw',
            'ht_sheets',
            'delivery_time',
            'remark',
            'add_time',
            'update_time',
            'admin_id',

        ];


//        $head = array('id', 'name');//设置表头
//        $keys = ['id','name'];//数据中表头对应的字段，用于读取相应数据
//        var_dump($data);
        excel_out('合同导出' . date('ymdhis', time()), $data, $head, $keys);

    }
}